package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.Entity.*;
import com.example.demo.Service.*;
import com.example.demo.utils.MD5Utils;
import com.example.demo.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class controller {
    @Autowired
    public UserService userService;
    @Autowired
    public ParcelService parcelService;
    @Autowired
    public CourierService courierService;
    @Autowired
    public DeliveryService deliveryService;

    //
//    @RequestMapping("/get")
//    public User get(String name) {
//        return userService.getUserByUsername(name);
//    }
//
    //查询所有用户-
    @CrossOrigin
    @RequestMapping("/user/getAllUser")
    public List<User> getAllUser() {
        return userService.getAllUser();
    }

    //模糊查询用户
    @CrossOrigin
    @RequestMapping("/user/userselect")
    public List<User> getUserBystr(@RequestBody JSONObject json) {
        String name = json.getString("name");
        return userService.getUserBystr(name);
    }

    /*
     * 查询快递
     */
    @CrossOrigin
    @RequestMapping("/getparcel")
    public Parcel getParcel() {
        System.out.println(parcelService.getParcel());
        return parcelService.getParcel();
    }

    //登录-
    @CrossOrigin
    @PostMapping("/login")
    public Message login(@RequestBody JSONObject json) {
        String username = json.getString("username");
        String password = json.getString("password");
        password = MD5Utils.md5(password);
        User user = userService.getUserByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                return Message.success(user);
            } else
                return Message.unsuccess("密码错误","/login");
        } else
            return Message.unsuccess("账号错误","/login");
    }

    //注册-
    @CrossOrigin
    @RequestMapping("/register")
    public User regist(@RequestBody JSONObject json, HttpServletResponse response) {
        String name = json.getString("username");
        String password = json.getString("password");
        String phone = json.getString("phone");
        String idcode = json.getString("idcode");
        String address = json.getString("address");
        Integer r_id = json.getIntValue("r_id");
        password = MD5Utils.md5(password);
        idcode = MD5Utils.md5(idcode);
        System.out.println(json);
        User user = new User();
        if (name == null || password == null) {
            return null;//用户名或密码为空
        } else if (userService.getUser(name) != null) {
            return null;//重名
        } else {
            if (r_id == 1) {
                user.setName(name);
                user.setPhone(phone);
                user.setIdcode(idcode);
                user.setAddress(address);
                user.setPassword(password);
                user.setR_id(r_id);
                Integer key_id = userService.setAdmin(user);
                System.out.println(key_id);
                user.setId(key_id);
                return user;//管理员注册
            } else if (r_id == 2) {
                user.setName(name);
                user.setPhone(phone);
                user.setIdcode(idcode);
                user.setAddress(address);
                user.setPassword(password);
                user.setR_id(r_id);
                Integer key_id = userService.setEmployee(user);
                System.out.println(key_id);
                user.setId(key_id);
                return user;//员工注册
            } else if (r_id == 3) {
                user.setName(name);
                user.setPhone(phone);
                user.setIdcode(idcode);
                user.setAddress(address);
                user.setPassword(password);
                user.setR_id(r_id);
                Integer key_id = userService.setUser(user);
                System.out.println(key_id);
                user.setId(key_id);
                return user;//用户注册
            }
            return null;//未选择角色;
        }
    }

    //根据id修改信息-
    @CrossOrigin
    @RequestMapping("/user/updateUser")
    public Message UpdateUserByid(@RequestBody JSONObject json) {
//        String name = json.getString("name");
        Integer r_id = json.getIntValue("r_id");
        Integer id = json.getIntValue("u_id");
        String phone = json.getString("phone");
//        String idcode = json.getString("idcode");
        String password = json.getString("password");
        String address = json.getString("address");
        System.out.println(id + password + address + phone + r_id);
        password = MD5Utils.md5(password);
//        idcode = MD5Utils.md5(idcode);
        if (r_id == 3) {
            User user = new User();
            user.setId(id);
//            user.setName(name);
            user.setPhone(phone);
//            user.setIdcode(idcode);
            user.setAddress(address);
            user.setPassword(password);
            user.setR_id(r_id);
            userService.updateUserByid(user);
            System.out.println(userService.updateUserByid(user));
            return Message.success(user);//修改用户成功
        }
        if (r_id == 2) {
            User user = new User();
            user.setId(id);
//            user.setName(name);
            user.setPhone(phone);
//            user.setIdcode(idcode);
            user.setAddress(address);
            user.setPassword(password);
            user.setR_id(r_id);
            userService.updateEmployeeByid(user);
            System.out.println(userService.updateEmployeeByid(user));
            return Message.success(user);//修改员工
        }
        if (r_id == 1) {
            User user = new User();
            user.setId(id);
//            user.setName(name);
            user.setPhone(phone);
//            user.setIdcode(idcode);
            user.setAddress(address);
            user.setPassword(password);
            user.setR_id(r_id);
            userService.updateAdminByid(user);
            System.out.println(userService.updateAdminByid(user));
            return Message.success(user);//修改管理员
        } else
            return Message.unsuccess("修改失败","/update/updateUser");
    }

    //员工模糊查询-
    @CrossOrigin
    @RequestMapping("/employee/employeeselect")
    public List<User> selectUser(@RequestBody JSONObject json) {
        String name = json.getString("name");
        System.out.println(userService.selectUser(name));
        return userService.selectUser(name);
    }

    //查询所有员工-
    @CrossOrigin
    @RequestMapping("/employee/getAllemployee")
    public List<User> getAllemployeeByid() {
        return userService.getAllemployee();
    }

    //按用户id获取快递信息-
    @CrossOrigin
    @RequestMapping("/parcel/getParcelByid")
    public List<Parcel> getParcelByid(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        System.out.println(parcelService.getParcelByid(id));
        return parcelService.getParcelByid(id);
    }

    //根据用户名获取快递信息
    @CrossOrigin
    @RequestMapping("/parcel/getParcelByname")
    public List<Parcel> getParcelByname(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        User user = userService.getUserByid(id, r_id);
        String name = user.getName();
        return parcelService.getParcelByname(name);
    }

    @CrossOrigin
    @RequestMapping("/delivery/getDeliveryByname")
    public List<Delivery> getDeliveryByname(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        User user = userService.getUserByid(id, r_id);
        String name = user.getName();
        return deliveryService.getDeliveryByname(name);
    }

    @CrossOrigin
    @RequestMapping("/courier/getCourierByname")
    public List<Courier> getCourierByname(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        User user = userService.getUserByid(id, r_id);
        String name = user.getName();
        return courierService.getCourierByname(name);
    }

    //用户传递预约寄件信息
    @CrossOrigin
    @RequestMapping("/courier/userSendCourierInfo")
    public Courier userSendCourierInfo(@RequestBody JSONObject json) {
        String s_sname = json.getString("s_sname");
        String s_rname = json.getString("s_rname");
        String s_saddress = json.getString("s_saddress");
        String s_raddress = json.getString("s_raddress");
        String s_sphone = json.getString("s_sphone");
        String s_rphone = json.getString("s_rphone");
        String s_idcode = json.getString("s_idcode");
        Integer s_u_id = json.getIntValue("u_id");
        Integer s_state = 0;
        Courier courier = new Courier();
        courier.setS_sname(s_sname);
        courier.setS_rname(s_rname);
        courier.setS_saddress(s_saddress);
        courier.setS_raddress(s_raddress);
        courier.setS_sphone(s_sphone);
        courier.setS_rphone(s_rphone);
        courier.setS_idcode(s_idcode);
        courier.setS_u_id(s_u_id);
        courier.setS_state(s_state);
        return courierService.userSendCourierInfo(courier);
    }

    //获取快递
    @CrossOrigin
    @RequestMapping("/delivery/getDelivery")
    public List<Delivery> getDelivery() {
        return deliveryService.getDelivery();
    }

    //根据id获取用户
    @CrossOrigin
    @RequestMapping("/user/getUser")
    public User getUserByid(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        return userService.getUserByid(id, r_id);
    }

    //寄件查询
    @CrossOrigin
    @RequestMapping("/courier/getCourier")
    public List<Courier> getShipByid(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("u_id");
        Integer r_id = json.getIntValue("r_id");
        User user = userService.getUserByid(id, r_id);
        String name = user.getName();
        System.out.println(courierService.getCourier());
        return courierService.getUserCourier(name);
    }

    //获取已寄出的快递
    @CrossOrigin
    @RequestMapping("/courier/getCouriered")
    public List<Courier> getCouriered() {
        return courierService.getCouriered();
    }

    //员工录入寄件信息
    @CrossOrigin
    @RequestMapping("/courier/inputCourierInfo")
    public Courier inputCourierInfo(@RequestBody JSONObject json) {

        Integer s_company = json.getIntValue("s_company");
        String s_number = json.getString("s_number");
        Integer s_expenses = json.getIntValue("s_expenses");
        Integer p_id = json.getIntValue("p_id");
        String name = courierService.getSendnameBypid(p_id);
        Integer s_u_id = userService.getUserU_id(name);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        Integer s_state = 1;
        return courierService.inputCourierInfo(s_number, s_company, s_expenses, format, s_state, p_id, s_u_id);
    }

    //获取员工未处理的快递
    @CrossOrigin
    @RequestMapping("/courier/getNotProCourier")
    public List<Courier> getNotProCourier() {
        return courierService.getNotProCourier();
    }

    //员工获取所有快递信息
    @CrossOrigin
    @RequestMapping("/parcel/getAllParcel")
    public List<Parcel> getAllParcel() {
        return parcelService.getAllParcel();
    }

    //录入快递取件信息
    @CrossOrigin
    @RequestMapping("/parcel/pickup")
    public Delivery inputDelivery(@RequestBody JSONObject json) {
        Integer p_id = json.getIntValue("express_id");
        Integer p_u_id = parcelService.getU_id(p_id);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        System.out.println(format);
        String p_rnumber = parcelService.getp_rnumber(p_id);
        String p_name = parcelService.getParcelname(p_id);
        parcelService.pickUp(p_id);
        return deliveryService.inputDelivery(p_u_id, p_id, format, p_rnumber, 1, p_name);
    }

//    //用户取件
//    @CrossOrigin
//    @RequestMapping("/parcel/pickup")
//    public void pickUp(@RequestBody JSONObject json) {
//        Integer p_id = json.getIntValue("express_id");
//
//    }

    //根据名字模糊查询快递
    @CrossOrigin
    @RequestMapping("/delivery/getDeliverymohuByname")
    public List<Delivery> getDelivery(@RequestBody JSONObject json) {
        String name = json.getString("name");
        System.out.println(deliveryService.getDeliverymohuByname(name));
        return deliveryService.getDeliverymohuByname(name);
    }

    //录入入站信息
    @CrossOrigin
    @RequestMapping("/parcel/inputParcel")
    public Parcel inputParcel(@RequestBody JSONObject json) {
        Integer p_id = json.getIntValue("p_id");
        String p_sname = json.getString("p_sname");
        String p_sphone = json.getString("p_sphone");
        String p_saddress = json.getString("p_saddress");
        String p_rname = json.getString("p_rname");
        String p_rphone = json.getString("p_rphone");
        String p_raddress = json.getString("p_raddress");
        String rec_number = json.getString("rec_number");
        Integer p_state = json.getIntValue("p_state");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        Integer u_id = userService.getUserU_id(p_rname);
        Parcel parcel = new Parcel();
        parcel.setP_id(p_id);
        parcel.setP_sname(p_sname);
        parcel.setP_rname(p_rname);
        parcel.setP_sphone(p_sphone);
        parcel.setP_rphone(p_rphone);
        parcel.setP_raddress(p_raddress);
        parcel.setP_saddress(p_saddress);
        parcel.setRec_number(rec_number);
        parcel.setP_state(p_state);
        parcel.setP_time(format);
        parcel.setU_id(u_id);
        return parcelService.inputParcel(parcel);
    }

    //用户修改寄件信息
    @CrossOrigin
    @RequestMapping("/parcel/updateParcel")
    public List<Courier> updateParcel(@RequestBody JSONObject json) {
        Integer p_id = json.getIntValue("p_id");
        String s_sname = json.getString("s_sname");
        String s_rname = json.getString("s_rname");
        String s_saddress = json.getString("s_saddress");
        String s_raddress = json.getString("s_raddress");
        String s_sphone = json.getString("s_sphone");
        String s_rphone = json.getString("s_rphone");
        String s_idcode = json.getString("s_idcode");
        Integer s_u_id = json.getIntValue("u_id");
        return courierService.updateCourier(p_id, s_sname, s_sphone, s_saddress, s_idcode, s_rname, s_rphone, s_raddress);
    }

    //用户删除预约信息
    @CrossOrigin
    @RequestMapping("/courier/deleteCourier")
    public Integer deleteCourier(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("p_id");
        System.out.println(id);
        if (courierService.deleteCourier(id) != 0) {
            return 1;
        } else
            return 0;
    }

    //按当前时间统计收入快递站的快递数目
    @CrossOrigin
    @RequestMapping("/parcel/getParcelCount")
    public List<Count> getParcelCount() {
        System.out.println(parcelService.getParcelCount());
        return parcelService.getParcelCount();
    }

    //按当前时间统计收入快递站的快递数目
    @CrossOrigin
    @RequestMapping("/parcel/getSendCount")
    public List<Count> getSendCount() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        System.out.println(format);
        return courierService.getSendCount();
    }

    //统计api
    @CrossOrigin
    @RequestMapping("/count")
    public ArrayList count() {
        ArrayList array = new ArrayList();
        array.add(deliveryService.getCountDelivery());
        array.add(parcelService.getParcelCount());
        array.add(courierService.getSendCount());
        return array;
    }

    //修改员工密码
    @CrossOrigin
    @RequestMapping("/update/updateEmployeePassword")
    public int updateEmployeePassword(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("id");
        String password = json.getString("update_employee_password");
        password = MD5Utils.md5(password);
        if (userService.updateEmployeePassword(id, password) != 0) {
            return 1;
        } else return 0;
    }

    //修改用户密码
    @CrossOrigin
    @RequestMapping("/update/updateUserPassword")
    public int updateUserPassword(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("id");
        String password = json.getString("update_user_password");
        password = MD5Utils.md5(password);
        if (userService.updateUserPassword(id, password) != 0) {
            return 1;
        } else return 0;
    }

    //删除员工
    @CrossOrigin
    @RequestMapping("/delete/deleteEmployee")
    public Integer deleteEmployee(@RequestBody JSONObject json) {
        Integer id = json.getIntValue("id");
        User user = userService.getUserByid(id, 2);
        String name = user.getName();
        String phone = user.getPhone();
        String idcode = user.getIdcode();
        String address = user.getAddress();
        String password = user.getPassword();
        Integer r_id = 1;
        User user1 = new User();
        user1.setR_id(r_id);
        user1.setPassword(password);
        user1.setAddress(address);
        user1.setIdcode(idcode);
        user1.setPhone(phone);
        user1.setName(name);
        return userService.setUser(user1);
    }

    //用户个数
    @CrossOrigin
    @RequestMapping("/count/countUser")
    public Integer countUser() {
        return userService.countUser();
    }

    //员工个数
    @CrossOrigin
    @RequestMapping("/count/countEmployee")
    public Integer countEmployee() {
        return userService.countEmployee();
    }

    //管理员个数
    @CrossOrigin
    @RequestMapping("/count/countAdmin")
    public Integer countAdmin() {
        return userService.countAdmin();
    }
}
